package com.peaksport.pkms.learn.day21.edgeservice;

import javax.ws.rs.core.Response.Status;

import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.common.rest.filter.HttpServerFilter;
import org.apache.servicecomb.core.Invocation;
import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
import org.apache.servicecomb.swagger.invocation.Response;
import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;

public class Day21AuthenticationFilter implements HttpServerFilter {

	public Response afterReceiveRequest(Invocation invocation, HttpServletRequestEx request) {
		String userName = request.getHeader("userName");
		String pwd = request.getHeader("pwd");
		if (!StringUtils.isEmpty(userName) && StringUtils.isEmpty(pwd))
			return Response.consumerFailResp(
					new InvocationException(Status.UNAUTHORIZED, new CommonExceptionData("账号密码不能为空."))
					);
		invocation.addContext("userName",userName);
		invocation.addContext("pwd",pwd);
		return null;
	}

	public int getOrder() {
		return 0;
	}

}
